<?php

App::uses('AdminController', 'Controller');

/**
 * Users Controller
 *
 * @property User $User
 * @property RequestHandlerComponent $RequestHandler
 */
class UsersController extends AdminController {

    /**
     * Helpers
     *
     * @var array
     */
    public $helpers = array(
        'Js',
        'Form' => array(
            'className' => 'BootstrapForm'
        ),
        'Html',
        'Session' => array(
            'className' => 'BootstrapSession'
        ),
        'Paginator' => array(
            'className' => 'BootstrapPaginator'
        )     
    );

/**
 * Components
 *
 * @var array
 */
    public $components = array('RequestHandler', 'Auth', 'Session');

    public function beforeFilter() {

        parent::beforeFilter();
        $this->Auth->allow('login', 'logout');
    }

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    /**
     * admin_index method
     *
     * @return void
     */
    public function admin_index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    /**
     * admin_view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
    }

    /**
     * admin_edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
        }
    }

    /**
     * admin_delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->User->delete()) {
            $this->Session->setFlash(__('User deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

    /**
     * admin/user/login
     * 
     * @return type
     */
    public function admin_login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                if ($this->Auth->user('is_admin')) {
                    return $this->redirect(array(
                                'controller' => 'users',
                                'action' => 'index',
                                'admin' => true
                            ));
                } else {
                    return $this->redirect('/');
                }
            } else {
                $this->Session->setFlash(__('Nombre de usuario o password incorrectos'), 'default', array(), 'auth');
            }
        }
    }

    public function admin_logout() {
        $this->redirect($this->Auth->logout());
    }

}
